/*******************************************************************************
*   				               DOFILE: RESHAPE FOR MHT					                       *
********************************************************************************

    **  OUTLINE:

        1. Reshaping the data to allow for using rwolf command


    **  NOTES:

        - The data on which the regressions and multiple hypotheses test corrections
          are run is a reshaped version of the main constructed data file.
          The reason for this is that the rwolf command (or any other mht
          correction command) does not allow for regressions with different
          controls. To overcome this issue all control variables are always included
          but are zero (and thus ommitted) when the control variable does not apply.



********************************************************************************/


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  * Load dataset
  use "$Endline_dtFin/data_angolacbm.dta", clear


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* 1. Reshaping the data to allow for using rwolf command

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


  // Select all variables that we need for analysis besides controls

  global other /// all 'other' variables that need to remain
  school_id teacher_id parent_id T0 T1 T2 T3 V block t regparent regteacher regschool parent_id teacher_id test_id

  global pY /// parent level dependent variables
  parentmob1_z parentmob2_z P_school_good_z parentsonM_z parentonT_z P_perf_student_z ///
      P_homework P_dinner P_rules P_sleep ///
      P_talk_teacher P_info P_parti P_perf_student ///
      P_problem17 P_director_respons P_school_good ///
      P_problem4 P_problem6 P_problem7 P_problem8 P_problem28 P_problem29 P_problem30 P_problem31 P_problem32 ///
      P_problem19 P_problem21 P_problem22 P_teacher_good P_perf_teachers ///
      P_homework_z P_dinner_z P_rules_z P_sleep_z ///
      Gp_tg Gp_pgg Gp_dgf0_2 Gp_dgf0_3

  global pgY /// parents games
      Gp_tg Gp_pgg Gp_dgf0_2 Gp_dgf0_3


  global tY /// teacher level dependent variables
  parentmobT_z pres_teacher ///
      T_parents_involvement T_problem16 T_parents_availablity p_inmeeting T_perf_comm ///
      T_problem4 T_problem6 T_problem7 T_problem8 T_problem28 T_problem29 T_problem30 T_problem31 T_problem32 ///
      pres_teacher1 pres_teacher2 ///
      Gt_dg_r1 Gt_dg_r2 Gt_dg_r3 Gt_dg_r Gt_tg Gt_pgg

  global tgY /// teachers games
  Gt_dg_r1 Gt_dg_r2 Gt_dg_r3 Gt_dg_r Gt_tg Gt_pgg

  global sY /// school level dependent variables
  formalparti_z formalparti_rr_z onF_z publicinfo_z safety_z higiene_z material_z ///
    S_comm_members S_comm S_board_parent_rep ///
    S_info1 S_info2 S_info3 S_info4 S_info5 S_info6 S_info7 S_info8 ///
    I_classrooms_improvement I_security I_fence I_fence_impr_const ///
    I_cleaner bathroom_students A_water ///
    I_chair_desk_usable_sh S_new_exerbooks S_new_chalk ///
    A_repeat_share A_students

  global dY /// director level dependent variables
  directoronT_z ///
  D_problem4 D_problem6 D_problem7 D_problem8 D_problem28 D_problem29 D_problem30 D_problem31 D_problem32 ///
  D_perf_teachers D_problem19 D_problem21 D_problem22

  global stY1 /// student level (1) dependent variables
  pres_stud1

  global stY2 /// student level (2) dependent variables (because these have two additional control variables for tests)
  test_34_z test_3_z test_4_z ///
  test_pt3 test_mat3 test_pt4 test_mat4

  global hetero ///
  T1xtop_perf T2xtop_perf T3xtop_perf top_perf ///
  T1xlow_educ T2xlow_educ T3xlow_educ low_educ ///
  T1xP_female T2xP_female T3xP_female



  // Define controls
  global sX		/// school controls
  teachers classrooms students urban

  global sXb /// school controls
  teachersbaseline classroomsbaseline studentsbaseline

  global pX		/// parent controls
  P_age P_female P_primedu P_partner P_children P_student_age P_student_female ///
  P_parent P_student_grade_3 P_student_grade_4 P_student_grade_5


  global tX		/// teacher controls
  T_age T_female T_highedu T_partner T_children T_teacher_preschool 	///
  T_teacher_grade_1 T_teacher_grade_2 T_teacher_grade_3 T_teacher_grade_4 ///
  T_teacher_grade_5 T_teacher_grade_6 T_teacher_grade_7 T_teacher_grade_8 ///
  T_teacher_grade_9  T_teacher_module1 T_teacher_module2 T_teacher_module3 T_exp_teach

  global dX		/// director controls
  D_age D_female D_highedu D_partner D_children

  global stX   /// student test controls
  test_grade test_type

  global pgX /// teacher game controls
  P_age P_female P_primedu P_partner P_children Gp_knows_teachers Gp_knows_parents

  global tgX /// teacher game controls
  T_age T_female T_highedu T_partner T_children Gt_knows_teachers Gt_knows_parents

  // keep all selected variables

  keep $sX $sXb $pX $tX $dX $stX $pgX $tgX $pY $tY $tgY $pgY $sY $dY $stY1 $stY2 $hetero $other

  // keep only endline

  keep if t==1

  save "$Endline_dtInt/MHT.dta", replace

  // Create independent datasets by level of observation

    // Parents
    use "$Endline_dtInt/MHT.dta", clear

    keep if regparent==1

    keep $sX $sXb $pX $pY school_id T0 T1 T2 T3 V block $hetero parent_id

    order school_id T0 T1 T2 T3 V block $sXb $pX $pY $hetero

    save "$Endline_dtInt/MHTparents.dta", replace

    // Parents games
    use "$Endline_dtInt/MHT.dta", clear

    keep if regparent==1

    keep $sX $sXb $pgX $pgY school_id T0 T1 T2 T3 V block $hetero parent_id

    order school_id T0 T1 T2 T3 V block $sXb $pgX $pgY $hetero

    rename P_* GP_*

    global pgX2 /// parent game controls
    GP_age GP_female GP_primedu GP_partner GP_children Gp_knows_teachers Gp_knows_parents


    save "$Endline_dtInt/MHTparentsgames.dta", replace

    // Teachers
    use "$Endline_dtInt/MHT.dta", clear

    keep if regteacher==1

    keep $sX $sXb $tX $tY school_id T0 T1 T2 T3 V block $hetero teacher_id

    order school_id T0 T1 T2 T3 V block $sXb $tX $tY $hetero

    save "$Endline_dtInt/MHTteachers.dta", replace

    // Teachers games
    use "$Endline_dtInt/MHT.dta", clear

    keep if regteacher==1

    keep $sX $sXb $tgX $tgY school_id T0 T1 T2 T3 V block $hetero teacher_id

    order school_id T0 T1 T2 T3 V block $sXb $tgX $tgY $hetero

    rename T_* GT_*

    global tgX2 /// teacher game controls
    GT_age GT_female GT_highedu GT_partner GT_children Gt_knows_teachers Gt_knows_parents

    save "$Endline_dtInt/MHTteachersgames.dta", replace


    // Schools
    use "$Endline_dtInt/MHT.dta", clear

    keep if regschool==1

    keep $sX $sXb $sY school_id T0 T1 T2 T3 V block $hetero

    order school_id T0 T1 T2 T3 V block $sXb $sY $hetero

    save "$Endline_dtInt/MHTschool.dta", replace

    // Director
    use "$Endline_dtInt/MHT.dta", clear

    keep if regschool==1

    keep $sX $sXb $dX $dY school_id T0 T1 T2 T3 V block $hetero

    order school_id T0 T1 T2 T3 V block $sXb $dX $dY $hetero

    save "$Endline_dtInt/MHTdirector.dta", replace

    // Students1
    use "$Endline_dtInt/MHT.dta", clear

    keep if pres_stud1!=.

    keep $sX $sXb $stY1 school_id T0 T1 T2 T3 V block $hetero parent_id

    rename parent_id student_id2

    order school_id T0 T1 T2 T3 V block $sXb $stY1 $hetero

    save "$Endline_dtInt/MHTstudents1.dta", replace

    // Students2
    use "$Endline_dtInt/MHT.dta", clear

    keep if test_34_z!=.

    keep $sX $sXb $stY2 school_id T0 T1 T2 T3 V block $stX $hetero test_id

    order school_id T0 T1 T2 T3 V block $sXb $stX $stY2 $hetero

    save "$Endline_dtInt/MHTstudents2.dta", replace

  // Append everything together

  use "$Endline_dtInt/MHTparents.dta", clear

  append using  "$Endline_dtInt/MHTteachers.dta"  /// 1
                "$Endline_dtInt/MHTschool.dta"  ///  2
                "$Endline_dtInt/MHTdirector.dta"  ///  3
                "$Endline_dtInt/MHTstudents1.dta"  /// 4
                "$Endline_dtInt/MHTstudents2.dta"  /// 5
                , gen(source)

                *"$Endline_dtInt/MHTteachersgames.dta"   /// 6
                *"$Endline_dtInt/MHTparentsgames.dta"   /// 7

  order source


  // Set controls to zero if they are not
    /*  Not doing this will brake the loop because of missing values,
        when all observations are zero it will be simply ommitted. */

  foreach var in $pX{
    replace `var' = 0 if source != 0
  }

  foreach var in $tX{
    replace `var' = 0 if source != 1
  }

  foreach var in $dX{
    replace `var' = 0 if source != 3
  }

  foreach var in $stX{
    replace `var' = 0 if source != 5
  }

  *foreach var in $tgX2{
  *  replace `var' = 0 if source != 6
  *}

  *foreach var in $pgX2{
  *  replace `var' = 0 if source != 7
  *}


  order school_id source parent_id teacher_id student_id2 test_id

  sort school_id source test_id student_id2 parent_id teacher_id

  save "$Endline_dtFin/data_angolacbm_MHT.dta", replace

****** End of dofile
